package com.sx.micowell.module.smartDevice;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.mvvm.bar.TitleBar;
import com.example.mvvm.base.ContainerActivity;
import com.example.mvvm.base.action.TitleBarAction;
import com.example.mvvm.base.data.DataBindingConfig;
import com.gyf.immersionbar.ImmersionBar;
import com.miloyu.modelnet.response.SmartDeviceTypeVO;
import com.sx.micowell.BR;
import com.sx.micowell.R;
import com.sx.micowell.databinding.FragSelsecAddDeviceBinding;
import com.sx.micowell.framework.app.MyFragment;
import com.sx.micowell.framework.arouth.RouteManager;
import com.sx.micowell.framework.arouth.RoutePath;
import com.sx.micowell.framework.model.smartDevice.AddDeviceListModel;
import com.sx.micowell.module.smartDevice.adapter.DeviceTypeListAdapter;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IBleActivator;
import com.tuya.smart.sdk.bean.BleActivatorBean;


/**
 * @Author: YY
 * @Time: 2021/9/16
 * @Describe 选择要添加的设备
 */
@Route(path = RoutePath.SMART_SELECT_ADD_DEVICE)
public class SelectAddDeviceFragment extends MyFragment<FragSelsecAddDeviceBinding, ContainerActivity>
                                                                                implements TitleBarAction {
    private AddDeviceListModel viewModel;

    private DeviceTypeListAdapter deviceTypeListAdapter;

    @Override
    protected void initViewModel() {
        super.initViewModel();
        viewModel = getFragmentScopeViewModel(AddDeviceListModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.frag_selsec_add_device, BR.viewModel, viewModel);
    }

    @Override
    protected void initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
                .fitsSystemWindows(true).init();
        getBinding().title.setOnTitleBarListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViewObservable() {
        super.initViewObservable();
        setDeviceTypeList();
    }

    @Nullable
    @Override
    public TitleBar getTitleBar() {
        return getBinding().title;
    }

    @Override
    public void onLeftClick(View v) {
        getMyActivity().finish();
    }

    public void setDeviceTypeList() {
        getBinding().recyclerDevice.setNestedScrollingEnabled(false);
        getBinding().recyclerDevice.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.deviceTypeData.observe(this, datas -> {
            if (datas != null && datas.size() > 0) {
                if (deviceTypeListAdapter == null) {
                    deviceTypeListAdapter = new DeviceTypeListAdapter(datas);
                    getBinding().recyclerDevice.setAdapter(deviceTypeListAdapter);

                    deviceTypeListAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter,
                                                @NonNull View view, int position) {
                            startContainerActivity(
                                    RouteManager.Companion.getInstance()
                                    .getSearchDeviceFragment()
                                    .getClass().getCanonicalName(), null);
                        }
                    });
                }
            }
        });
    }


}
